package com.leyou.item.service;

import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exceptions.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.item.dto.CategoryDTO;
import com.leyou.item.entity.Category;
import com.leyou.item.mapper.CategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
public class CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    public List<CategoryDTO> queryCategoryByPid(Long pid) {

        Category record = new Category();
        record.setParentId(pid);
        //条件查询，根据对象的属性，动态生成，动态sql,标准生成为key=value 如果有多个值，则:where key=value and key=value
        List<Category> categories = this.categoryMapper.select(record);

        //判断集合是否为空
        if (CollectionUtils.isEmpty(categories)){
            throw new LyException(ExceptionEnum.CATEGORY_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(categories,CategoryDTO.class);
    }

    public List<CategoryDTO> queryCategoryByBrandId(Long bid) {

        List<Category> categories = this.categoryMapper.queryCategoryByBrandId(bid);

        if (CollectionUtils.isEmpty(categories)){
            throw new LyException(ExceptionEnum.DATA_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(categories,CategoryDTO.class);
    }


    //select * from tb_category where id in (1,2,3,4,6)
    public List<CategoryDTO> queryCategoryByIds(List<Long> categoryIds) {
        //根据主键id集合查询
        List<Category> categories = this.categoryMapper.selectByIdList(categoryIds);

        if (CollectionUtils.isEmpty(categories)){
            throw new LyException(ExceptionEnum.DATA_NOT_FOUND);
        }

        return BeanHelper.copyWithCollection(categories,CategoryDTO.class);
    }
}
